#!/usr/bin/env python
# coding=utf-8

from __future__ import print_function
import json
import easydict
import argparse
import os.path
import numpy as np
from sort.sort_joints import Sort
from sort.sort_joints import oneshot_15_pose


# set_name = 'train_v1'
# output_dir_name = '{}_mode_{}_result'.format(set_name, MODE)

def oneshot_15(det_path, output_path):
    # det_path = '/Users/gerrie/prj/end2end_mot/deep-sort/MOT16/train_v1/{}/det/det.txt'
    # det_path = '/Users/gerrie/prj/end2end_mot/tmp/det-{}.txt'
    # det_path = '/Users/gerrie/prj/end2end_mot/output/mymot_joints_det_{}.txt'
    # seq_dets = np.loadtxt(det_path.format(seq_name),delimiter=',') #load detections

    mot_tracker = Sort(iou_threshold=0.1)
    seq_dets = np.loadtxt(det_path, delimiter=',')  # load detections

    with open(output_path, 'w') as out_file:
        for frame in range(int(seq_dets[:, 0].max())):
            frame += 1  # detection and frame numbers begin at 1
            dets = seq_dets[seq_dets[:, 0] == frame, 2:7]
            dets[:, 2:4] += dets[:, 0:2]  # convert to [x1,y1,w,h] to [x1,y1,x2,y2]
            trackers = mot_tracker.update(dets)
            for d in trackers:
                tlwh_box = [d[0], d[1], d[2] - d[0], d[3] - d[1]]
                tid = d[4]
                # dd = estimate_box(tlwh_box)
                dd = tlwh_box
                print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' % (frame, tid, dd[0], dd[1], dd[2], dd[3]),
                      file=out_file)


def mot15_batch_handle():
    sequence_name_set = {'test': ['ADL-Rundle-1',
                                  'AVG-TownCentre',
                                  'ETH-Jelmoli',
                                  'KITTI-16',
                                  'PETS09-S2L2',
                                  'Venice-1',
                                  'ADL-Rundle-3',
                                  'ETH-Crossing',
                                  'ETH-Linthescher',
                                  'KITTI-19',
                                  'TUD-Crossing'], 'train': ['ADL-Rundle-6',
                                                             'ADL-Rundle-8',
                                                             'ETH-Bahnhof',
                                                             'ETH-Pedcross2',
                                                             'ETH-Sunnyday',
                                                             'KITTI-13',
                                                             'KITTI-17',
                                                             'PETS09-S2L1',
                                                             'TUD-Campus',
                                                             'TUD-Stadtmitte',
                                                             'Venice-2']}
    set_name = 'test'
    det_path = '../output/mot15_pose/{}/{}/det_pose.hdf5'
    output_dir_name = 'sort_joint/{}'.format(set_name)
    output_dir_name = 'sort_joint2/{}'.format(set_name)
    if not os.path.exists('../output/{}'.format(output_dir_name)):
        os.makedirs('../output/{}'.format(output_dir_name))

    for seq in sequence_name_set[set_name]:
        h5_input_path = det_path.format(set_name, seq)
        if not os.path.exists(h5_input_path):
            print('miss file ' + h5_input_path)
            continue
        output_path = '../output/{}/{}.txt'.format(output_dir_name, seq)
        oneshot_15_pose(h5_input_path, output_path)

def parse_args():
    """ Parse command line arguments.
    """
    parser = argparse.ArgumentParser(description="Deep SORT")
    parser.add_argument(
        "--pose_dir", help="Path to alphapose output directory",
        default=None, required=True)
    parser.add_argument((

    ))
    return parser.parse_args()

def run():
    args = parse_args()



if __name__ == '__main__':
    # seq_name = 'MOT16-02'
    # seq_name = 'PETS09-S2L1'
    # output_path = '../output/train_result/{}.txt'.format(seq_name)
    # oneshot_16(seq_name, output_path)

    # mot15_batch_df()
    mot15_batch_handle()
